package ws.performancetest.api;

import org.hyperic.sigar.CpuInfo;
import org.hyperic.sigar.Mem;
import org.hyperic.sigar.NetInfo;
import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarException;
import org.hyperic.sigar.SysInfo;

/**
 * Represents system information of a computational node.
 *
 * <p>Information gathered:</br>
 *  <ul>
 *    <li>Host name.
 *    <li>CPU vendor, model and number of cores.
 *    <li>RAM amount.
 *    <li>Operating System.
 *  </ul>
 * </p>
 * 
 * @author Wilson Santos, 36228 <wilson.santos@ua.pt>
 */
public class SystemInfo {

  /** Host name. */
  protected String name;
  /** CPU vendor, model and number of cores. */
  protected String cpu;
  /** RAM amount. */
  protected String ram;
  /** Operating System. */
  protected String operatingSystem;

  /**
   * Create new instance.
   *
   * If your choose to postpone the information gathering, you can then use
   * the method {@link #gatherSystemInformation()}.
   *
   * @param autogather if {@code true} the system information will be gathered
   * automatically on object creation, otherwise the object if created will {@code NULL}
   * information.
   * 
   * @throws SigarException
   */
  public SystemInfo(boolean autogather) throws SigarException {
    if(autogather) {
      gatherSystemInformation();
    }
  }
  
  /**
   * Gather system information.
   *
   * @throws SigarException
   */
  public final void gatherSystemInformation() throws SigarException {
    Sigar sigar = new Sigar();
    CpuInfo info = sigar.getCpuInfoList()[0];
    Mem mem = sigar.getMem();
    SysInfo sysinfo = new SysInfo();
    sysinfo.gather(sigar);
    NetInfo netinfo = new NetInfo();
    netinfo.gather(sigar);

    name = netinfo.getHostName();
    cpu = info.getVendor() + " " + info.getModel() + " (" + info.getTotalCores() + " cores)";
    ram = mem.getRam() + " MB";
    operatingSystem = sysinfo.getName() + " " + sysinfo.getDescription() + " - " + sysinfo.getMachine();
  }

  public String getCpu() {
    return cpu;
  }

  public void setCpu(String cpu) {
    this.cpu = cpu;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public String getOperatingSystem() {
    return operatingSystem;
  }

  public void setOperatingSystem(String operatingSystem) {
    this.operatingSystem = operatingSystem;
  }

  public String getRam() {
    return ram;
  }

  public void setRam(String ram) {
    this.ram = ram;
  }

  @Override
  public String toString() {
    return "SystemInfo{" + "\n\tname=" + name + "\n\tcpu=" + cpu + "\n\tram=" + ram + "\n\toperatingSystem=" + operatingSystem + '}';
  }
}
